package net.haveraid.util;

import java.beans.PropertyEditorSupport;

import org.joda.time.DateTime;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

public class CustomBindingInitializer implements WebBindingInitializer {

	@Override
	public void initBinder(WebDataBinder binder, WebRequest request) {
		binder.registerCustomEditor(DateTime.class, new DateTimePropertyEditor());
	}

	private static class DateTimePropertyEditor extends PropertyEditorSupport {

		@Override
		public String getAsText() {
			DateTime dt = (DateTime)getValue();
			return dt == null ? "" : dt.toString("yyyy-MM-dd");
		}

		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(DateTime.parse(text));
		}
		
	}

}
